package cn.chen.thread;

public class Tools {

    public static ThreadLocal t1 = new ThreadLocal();

}

class ThreadA extends Thread{
    @Override
    public void run() {
        for(int i = 0;i < 100; i ++){
            try {
                Tools.t1.set("ThreadA " + (i + 1));
                System.out.println("ThreadA get value " + Tools.t1.get());
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class ThreadB extends Thread{
    @Override
    public void run() {
        for(int i = 0;i < 100; i ++){
            try {
                Tools.t1.set("ThreadB " + (i + 1));
                System.out.println("ThreadB get value " + Tools.t1.get());
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Run{
    public static void main(String[] args) {
        try {
            ThreadA a = new ThreadA();
            ThreadB b = new ThreadB();
            a.start();
            b.start();

            for(int i = 0;i < 100; i++){
                Tools.t1.set("Main" + (i + 1));
                System.out.println("Main get Value = " + Tools.t1.get());
                Thread.sleep(200);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}